<%--
  Created by IntelliJ IDEA.
  User: yuan
  Date: 2022/11/24
  Time: 11:19
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加员工</title>
    <base href="<%=request.getContextPath()+"/"%>">
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript">

        $(function () {
            $.get("http://localhost:8080<%=request.getContextPath()%>/DeptServlet/listDept",
            function (data, status) {
                let result = JSON.parse(data);
                console.log(result);
                if (status==="success") {
                    let depts = result;
                    for (let dept in depts) {
                        $("select#deptno").append("<option value='"+ depts[dept].deptno + "'>"+depts[dept].dname+"</option>")
                    }
                } else {
                    alert(result.msg);
                }
              }
            );
        });

        $(function () {
            $("#btnAdd").click(function () {
                //统一获取表单数据并转为json
                let data = {};
                $($("#form").serializeArray()).each(function () {
                    data[this.name] = this.value;
                });
                let formData = JSON.stringify(data)
                $.post(
                    "http://localhost:8080<%=request.getContextPath()%>/EmpServlet/addEmpAsync", formData,
                    function (data, status) {
                        let result = JSON.parse(data);
                        if (result.code===200 && status==="success") {
                            window.location.href = "http://localhost:8080<%=request.getContextPath()%>/EmpServlet/list"
                        } else {
                            alert(result.msg);
                        }
                    });
            });
        });

    </script>
</head>
<body>
<form method="post" action="EmpServlet/list" id="form" onsubmit="return false">
    <table border="1">
        <tr>
            <td>姓名</td>
            <td><input type="text" id="ename" name="ename" value="admin"></td>
        </tr>

        <tr>
            <td>工资</td>
            <td><input type="text" id="sal" name="sal" value="13000.0"></td>
        </tr>

        <tr>
            <td>奖金</td>
            <td>
                <input type="text" id="comm" name="comm" value="1000.0">
            </td>
        </tr>

        <tr>
            <td>职位</td>
            <td>
                <input type="text" id="job" name="job" value="Java高级开发工程师">
            </td>
        </tr>

        <tr>
            <td>领导</td>
            <td>
                <input type="text" id="mgr" name="mgr" value="7369">
            </td>
        </tr>

        <tr>
            <td>部门</td>
            <td>
                <select name="deptno" id="deptno">
                </select>
            </td>
        </tr>

        <tr>
            <td>入职时间</td>
            <td>
                <input type="date" id="hiredate" name="hiredate" value="2022-11-23">
            </td>
        </tr>

        <tr>
            <td colspan="2">
                <button id="btnAdd">添加</button>
            </td>
        </tr>

    </table>
</form>

</body>
</html>
